var Pagination = function() {

};

Pagination.prototype = {
    render: function(options) {
        if(!options.url) {
            throw new Error("pagination -> url not found");
        }
        if(!options.target) {
            throw new Error("pagination -> target not found");
        }

        this.settings = {
            page : 1,
            limit : 20,
            delay : 500,
            autoScroll : true,
            hideBarWhenFirstLoading: true,
            loadingText: '加载中...',
            loadMoreText: '加载更多',
            loadMoreClickText : '点击加载更多'
        };

        this.isBusy = true;
        this.isCompleted = false;

        $.extend(this.settings, options);

        var params = options.params || {};
        this.params = params;

        this.makeLoadingBar();

        this.loadData();
        if(this.settings.autoScroll) {
            this.bindScrollEvent();
        }
    },

    bindScrollEvent: function() {
        var $target = $(document);
        $target.scroll(function() {
            if($target.scrollTop() + $(window).height() >= $target.height() && !this.isBusy && !this.isCompleted) {
                this.isBusy = true;
                console.log(9999)
                // show loading bar
                this.showLoadingBar(this.settings.loadingText);
                // load next page
                setTimeout(function() {
                    this.loadData();
                }.bind(this), this.delay);
            }
        }.bind(this));
    },

    appendParams: function(p) {
        for(var pro in p) {
            this.params[pro] = p[pro];
        }
    },

    loadData: function() {
        this.params.page = this.settings.page;
        this.params.limit = this.settings.limit;
        // this.params = Utils.getAUTHParams(this.settings.url, this.params);
        $.ajax({
            url: this.settings.url,
            type: 'GET',
            dataType: 'json',
            crossDomain: true,
            data: this.params,
            error: function(error) {
                // error
                this.showLoadingBar(this.settings.loadMoreClickText);
                this.isBusy = false;
                if(this.settings.callback) {
                    this.settings.callback([], this.settings.page);
                }
            }.bind(this),
            success: function(resp) {
                if(resp.success) {
                    // resp must be like : { count:?, items:? }
                    var result = resp.result;
                    if(result.items.length < this.settings.limit) {
                        // hide bar
                        this.hideLoadingBar();
                        this.isCompleted = true;
                    } else {
                        this.showLoadingBar(this.settings.loadMoreClickText);
                    }
                    if(this.settings.callback) {
                        this.settings.callback(result, this.settings.page);
                    }
                    this.settings.page++;
                } else {
                    // failed
                    this.showLoadingBar(this.settings.loadMoreClickText);
                    if(this.settings.callback) {
                        this.settings.callback(null, this.settings.page);
                    }
                }

                this.isBusy = false;
            }.bind(this)
        });
    },

    showLoadingBar: function(text) {
        if(text) {
            this.settings.target.find('.loading-bar').text(text).show();
        } else {
            this.settings.target.find('.loading-bar').show();
        }
    },

    hideLoadingBar: function() {
        this.settings.target.find('.loading-bar').hide();
    },

    makeLoadingBar: function() {
        if(this.settings.autoScroll) {
            // loading
            this.settings.target.append('<div class="loading-bar">' + this.settings.loadingText + '</div>');
        } else {
            this.settings.target.append('<div class="loading-bar">' + this.settings.loadMoreText + '</div>');
        }

        // load more click event
        this.settings.target.find('.loading-bar').click(function() {
            if(this.isBusy === false) {
                this.isBusy = true;
                // show loading bar
                this.showLoadingBar(this.settings.loadingText);
                // load next page
                this.loadData();
            }
        }.bind(this));

        if(this.settings.textStyle) {
            this.settings.target.find('.loading-bar').css(this.settings.textStyle);
        }

        if(this.settings.hideBarWhenFirstLoading) {
            this.hideLoadingBar();
        }
    },

    destroy: function(target) {
        target.find('.loading-bar').remove();
    }
};